﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UPNP.IO
{
    public static class DeviceConvert
    {
        public static object ToObject(object v)
        {
            if (v == null)
                return null;

            if (v is DBNull)
                return null;

            return v;
        }

        public static string ToString(object v)
        {
            if (v == null)
                return null;

            if (v is DBNull)
                return null;

            try
            {
                return Convert.ToString(v);
            }
            catch
            {
                return null;
            }
        }

        public static bool ToBoolean(object v)
        {
            if (v == null)
                return false;

            if (v is DBNull)
                return false;

            try
            {
                return Convert.ToBoolean(v);
            }
            catch
            {
                return false;
            }
        }

        public static char ToChar(object v)
        {
            if (v == null)
                return '\0';

            if (v is DBNull)
                return '\0';

            try
            {
                return Convert.ToChar(v);
            }
            catch
            {
                return '\0';
            }
        }

        public static UInt32 ToUInt32(object v)
        {
            if (v == null)
                return 0;

            if (v is DBNull)
                return 0;

            try
            {
                return Convert.ToUInt32(v);
            }
            catch
            {
                return 0;
            }
        }

        public static UInt16 ToUInt16(object v)
        {
            if (v == null)
                return 0;

            if (v is DBNull)
                return 0;

            try
            {
                return Convert.ToUInt16(v);
            }
            catch
            {
                return 0;
            }
        }

        public static Byte ToByte(object v)
        {
            if (v == null)
                return 0;

            if (v is DBNull)
                return 0;

            try
            {
                return Convert.ToByte(v);
            }
            catch
            {
                return 0;
            }
        }

        public static Int32 ToInt32(object v)
        {
            if (v == null)
                return 0;

            if (v is DBNull)
                return 0;

            try
            {
                return Convert.ToInt32(v);
            }
            catch
            {
                return 0;
            }
        }

        public static Int16 ToInt16(object v)
        {
            if (v == null)
                return 0;

            if (v is DBNull)
                return 0;

            try
            {
                return Convert.ToInt16(v);
            }
            catch
            {
                return 0;
            }
        }

        public static SByte ToSByte(object v)
        {
            if (v == null)
                return 0;

            if (v is DBNull)
                return 0;

            try
            {
                return Convert.ToSByte(v);
            }
            catch
            {
                return 0;
            }
        }

        public static Single ToSingle(object v)
        {
            if (v == null)
                return 0.0f;

            if (v is DBNull)
                return 0.0f;

            try
            {
                return Convert.ToSingle(v);
            }
            catch
            {
                return 0.0f;
            }
        }

        public static Double ToDouble(object v)
        {
            if (v == null)
                return 0.0;

            if (v is DBNull)
                return 0.0;

            try
            {
                return Convert.ToDouble(v);
            }
            catch
            {
                return 0.0;
            }
        }

        public static DateTime ToDateTime(object v)
        {
            if (v == null)
                return DateTime.MinValue;

            if (v is DBNull)
                return DateTime.MinValue;

            try
            {
                return Convert.ToDateTime(v);
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        public static TimeSpan ToTimeSpan(object v)
        {
            if (v == null)
                return TimeSpan.MinValue;

            if (v is DBNull)
                return TimeSpan.MinValue;

            try
            {
                var l = Convert.ToInt64(v);

                return new TimeSpan(l);
            }
            catch
            {
                return TimeSpan.MinValue;
            }
        }

        public static byte[] FromBase64String(object v)
        {
            if (v == null)
                return null;

            if (v is DBNull)
                return null;

            try
            {
                var s = Convert.ToString(v);

                if (s == null)
                    return null;

                return Convert.FromBase64String(s);
            }
            catch
            {
                return null;
            }
        }

        public static Guid ToGuid(object v)
        {
            if (v == null)
                return Guid.Empty;

            if (v is DBNull)
                return Guid.Empty;

            try
            {
                var s = Convert.ToString(v);

                if (s == null)
                    return Guid.Empty;

                return new Guid(s);
            }
            catch
            {
                return Guid.Empty;
            }
        }

        public static Uri ToUri(object v)
        {
            if (v == null)
                return null;

            if (v is DBNull)
                return null;

            try
            {
                var s = Convert.ToString(v);

                if (s == null)
                    return null;

                return new Uri(s);
            }
            catch
            {
                return null;
            }
        }

    }
}
